package com.lp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by lp
 * 2017/11/7 9:48
 */
@RestController
@Controller
@ResponseBody
public class GirlController {

    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    private GirlService girlService;

    /**
     * 添加女生
     * @return
     */
    @PostMapping("/girls")
    public Girl girlAdd(@RequestParam("cupSize") String cupSize,
                              @RequestParam("age") Integer age){
        Girl girl = new Girl();
        girl.setCupSize(cupSize);
        girl.setAge(age);
        return  girlRepository.save(girl);
    }

    /**
     * 删除女生
     * @return
     */
    @DeleteMapping(value="/girls/{id}")
    public void girlDelete(@PathVariable("id") Integer id){
        girlRepository.delete(id);
    }

    /**
     * 更新女生
     * @return
     */
    @PutMapping(value="/girls/{id}")
    public Girl girlUpdate(@PathVariable("id") Integer id,
                           @RequestParam("cupSize") String cupSize,
                           @RequestParam("age") Integer age){
        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);
        return  girlRepository.save(girl);
    }

    /**
     * 查询女生列表
     * @return
     */
    //@RequestMapping(value="/findAll", method=RequestMethod.GET)
    @GetMapping("/girls")
    public List<Girl> girlList(){
        return  girlRepository.findAll();
    }

    /**
     * 查询女生
     * @return
     */
    @GetMapping(value="/girls/{id}")
    public Girl girlFindOne(@PathVariable("id") Integer id){
        return  girlRepository.findOne(id);
    }

    /**
     * 通过年龄查询女生
     * @return
     */
    @GetMapping(value="/girls/age/{age}")
    public List<Girl> girlListByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }

    /**
     * 通过年龄查询女生
     * @return
     */
    @GetMapping(value="/girls/age")
    public List<Girl> girlListByAge2(@RequestParam("age") Integer age){
        return girlRepository.findByAge(age);
    }

    /**
     *
     * @return
     */
    @PostMapping(value="/girls/two")
    public void insertTwo(){
        girlService.insertTwo();
    }


}
